(function(){
	angular.module("webApp").controller("CooperationCtrl",CooperationCtrl);
	CooperationCtrl.$inject=['$scope','dataService','$location','$state','helper','$rootScope'];
	function CooperationCtrl($scope,dataService,$location,$state,helper,$rootScope){
		$scope.listAction=function(){
			$scope.page_Size=10;
			$scope.getcooperationList(1);
		}
		$scope.getcooperationList=function(page_No){
			var query={page_no:page_No,page_size:$scope.page_Size};
			dataService.supplerCooperationGet(query).then(function(result){
				if(result.Msg){
					helper.myAlert("error:"+result.Msg);
				}else{
					$scope.cooperations=result.shop;
					$scope.cooperationsTotal=result.total_count;
				}
			});
		}

		$scope.addAction=function(){
			$scope.cooperate_id=$location.search().cooperate_id;
			if($scope.cooperate_id){
				dataService.supplerCooperationOne($scope.cooperate_id).then(function(cooperation){
					$scope.cooperation=cooperation;
				})
			}else{
				$scope.cooperation={};
				// console.log($rootScope.$root.currentUser,$rootScope.getUserInfo(true),$scope,$rootScope);
				dataService.userCurrentGet(true).then(function(user) {
					$scope.cooperation.supplier_id=user.user_id;
					$scope.cooperation.supplier_nick=user.user_name;
				});
			}
			
		}
		$scope.addfenxiaograde=function(fenxiaograde){
			console.log(fenxiaograde);
			$scope.cooperation.grade_id=fenxiaograde.grade_id;
		}
		$scope.addfenxiao=function(distributor){
			$scope.cooperation.distributor_id=distributor.partner_id;
			$scope.cooperation.distributor_nick=distributor.user_name;
		}
		 $scope.addproductcat = function(productcat) {
		 	 console.log("ddd1", productcat);
		 	 	$scope.cooperation.product_line ="";
		 	 	$scope.cooperation.product_line_name="";
		 	 _.map(productcat,function(pro){
		 	 	    if(!$scope.cooperation.product_line){
		            	$scope.cooperation.product_line = pro.id+"";
		           	    $scope.cooperation.product_line_name=pro.name;
		            }else{
		            	var produ_id=[];
		            	if(_.isString($scope.cooperation.product_line)){
		            	 produ_id=$scope.cooperation.product_line.split(",");
		            	}else{
		            	 produ_id.push($scope.cooperation.product_line);
		            	}
		            	var repeat=true;
		            	async.map(produ_id,function(pid,callback){
		            		if(pid==pro.id){
		            			repeat=false;
		            		}
		            		callback(null,null);
		            		
		            	},function(e,r){
		            		if(repeat&&pro.id){
		            			$scope.cooperation.product_line +=',' +pro.id;
		           	    		$scope.cooperation.product_line_name+=','+pro.name;
		            		}
		            	});
		            }
		 	 });
        
            
        }
        $scope.infoAction=function(){
        	$scope.cooperation_id=$location.search().cooperation_id;
        	dataService.supplerCooperationOne($scope.cooperation_id).then(function(result){
        		if(result.Msg){
						helper.myAlert("error:"+result.Msg);
					}else{
						$scope.cooperation=result;
				}
        	});
        }
        $scope.cooperationDelete=function(cooperation_id){
        	dataService.supplerCooperationDelete(cooperation_id).then(function(result){
					if(result.Msg){
						helper.myAlert("error:"+result.Msg);
					}else{
						helper.myAlert("success",null,null,function(){
							$scope.listAction();
						});
					}
			})
        }
        $scope.empower=function(cooperation){
        	console.log(cooperation);
        	cooperation.supplier_agree=true;
        	// return false;
        	dataService.supplerCooperationUpdate(cooperation).then(function(r){
        		if(r.Msg){
        			helper.myAlert("error:"+r.Msg);
        		}else{
        			helper.myAlert("success","#/cooperation/list");
        		}
        	})
        }
		$scope.CooperationAdd=function(){
			if($scope.cooperation.cooperate_id){
				dataService.supplerCooperationUpdate($scope.cooperation).then(function(result){
					if(result.Msg){
						helper.myAlert("error:"+result.Msg);
					}else{
						helper.myAlert("success",null,null,function(){
							$state.go("cooperation.list");
						});
					}
				})
			}else{
				$scope.cooperation.supplier_agree = true;
				dataService.supplerCooperationAdd($scope.cooperation).then(function(result){
					if(result.Msg){
						helper.myAlert("error:"+result.Msg);
					}else{
						helper.myAlert("success",null,null,function(){
							$state.go("cooperation.list");
						});
					}
				});
			}
		}
	}
})();